﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.BLL.Basic.Stock.Facilities;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Infrastructure.Facilities;
using Gfw.Infrastructure.Stock.Facilities;
using Gfw.Infrastructure.User;
using Gfw.Repositories.Account;
using Gfw.Repositories.Admin;
using Gfw.Repository;

namespace Gfw.BLL.Basic.Facilities
{
    public class ActiveUserFacilityImpl : ActiveUserFacility
    {
        public ActiveUserFacilityImpl(string userId)
            : base(userId)
        {
        }

        public override void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();
            try
            {
                ActUserAccountinformationRepository accountRep = new ActUserAccountinformationRepository();
                var account = accountRep.Get(_userId, work);

                if (account.Status != (int) UserStatusEnum.NotActive)
                    throw new GfwException("用户已经激活。");

                if (account.ElectronicMoney < 560)
                    throw new GfwVerifyException("余额不足");


                //首先缴纳年费
                AnnualFee annualFee = new AnnualFee(account, YearFee);
                annualFee.Execute(work);

                //扣除动态奖金
                AccruedAward accruedAward = new AccruedAward(account, AwardFee);
                accruedAward.Execute(work);

                //剩下的工作，就是如何将200电子币，转换到股票币，并购买股票
                ToStockMoneyFacilityImpl tostock = new ToStockMoneyFacilityImpl(account, 200);
                tostock.Execute(work);

                //更改账户信息
                //account.ElectronicMoney -= 560;
                account.Status = (int) UserStatusEnum.WaitActive;
                accountRep.Update(account, work);

                //更改用户信息
                UsrUserRepository userRep = new UsrUserRepository();
                var user = userRep.Get(_userId, work);
                user.IsActivation = (int) UserStatusEnum.Normal;
                user.ActivationDate = DateTime.Now;
                userRep.Update(user, work);



                //以下是动态奖金的构成
                //直接推荐奖
                RecommendAward recommendAward = new RecommendAward(user.Recommender, 260*.15M);
                recommendAward.Execute(work);

                //计算管理将
                /* Modify by: Fan 2012-08-28
                ManagementAward maward = new ManagementAward(user.Id, 260);
                maward.Execute(work);
                 */

                //对碰奖

                if (unitOfWork == null)
                {
                    work.Commit();

                }
            
            }
            catch (Exception exp)
            {
                work.Rollback();
            }

        }
    }
}

